home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / pc / mac_file / vendor_d / neuralwa / nw2v50 / bsbhebb.c < prev    next >
Text File  |  1993-08-23  |  16KB  |  470 lines

  1. /* 15:30  04-Jul-88  (bsbhebb.c)   Brain-State-in-a-Box Hebb I/O */
  2.  
  3. /************************************************************************
  4.  * Copyright(C) 1987-1992 NeuralWare Inc                                *
  5.  * Penn Center West, IV-227, Pittsburgh, PA 15276                       *
  6.  * Telephone: (412) 787-8222    FAX: (412) 787-8220                     *
  7.  *                                                                      *
  8.  * All rights reserved.  No part of this program may be reproduced,     *
  9.  * stored in a retrieval system, or transmitted, in any form or by any  *
  10.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  11.  * without the prior written permission of the copyright owner,         *
  12.  * NeuralWare, Inc.                                                     *
  13.  *                                                                      *
  14.  *                          PROPRIETARY NOTICE                          *
  15.  *                                                                      *
  16.  * This document is the property of NeuralWare, Inc. and contains       *
  17.  * trade-secrets and other proprietary information.  The information    *
  18.  * herein is reserved as proprietary to NeuralWare, and is not to be    *
  19.  * published, reproduced, copied, disclosed, used, or reverse           *
  20.  * engineered without the express written consent of a duly authorized  *
  21.  * representative of NeuralWare.                                        *
  22.  ************************************************************************
  23.  */
  24.  
  25.  
  26. /************************************************************************
  27.  *                  *
  28.  *  Hebbian Learning for BSB (built-in data)      *
  29.  *                  *
  30.  ************************************************************************
  31.  */
  32. #include "userutl.h"
  33.  
  34. #ifndef SUN
  35. #ifndef DLC
  36. #include <stdlib.h>
  37. #endif
  38. #endif
  39.  
  40. #ifdef MAC
  41. #include "macuio.redef"
  42. #endif
  43.  
  44. /* Input arrays */
  45. static  int  curr_index[3] = {0}; /* indices into character arrays */
  46. static  int  num_words[3] = { 7, 3, 3 };/* # words in each category */
  47.  
  48. static  char  name[2][7][17] = {
  49. {
  50.   {'S','o','c','r','a','t','e','s',' ',' ',' ',' ',' ',' ',' ',' ',0},
  51.   {'A','l','c','i','b','i','a','d','e','s',' ',' ',' ',' ',' ',' ',0},
  52.   {'P','l','a','t','o',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  53.   {'Z','e','u','s',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  54.   {'A','p','o','l','l','o',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  55.   {'D','i','a','n','a',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  56.   {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0}
  57. },
  58. {
  59.   {'+','+','+','+','+','+','+','+','-','-','-','-','-','-','-','-',0},
  60.   {'-','-','-','-','+','+','+','+','-','-','-','-','+','+','+','+',0},
  61.   {'+','-','+','-','+','-','+','-','+','-','+','-','+','-','+','-',0},
  62.   {'-','-','+','+','-','-','+','+','-','-','+','+','-','-','+','+',0},
  63.   {'+','+','+','+','-','-','-','-','-','-','-','-','+','+','+','+',0},
  64.   {'+','-','-','+','+','-','-','+','+','-','-','+','+','-','-','+',0},
  65.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
  66. }};
  67. static  char  species[2][3][17] = {
  68. {
  69.   {'M','a','n',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  70.   {'G','o','d',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  71.   {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0}
  72. },
  73. {
  74.   {'+','-','+','-','+','-','+','-','+','-','+','-','+','-','+','-',0},
  75.   {'-','-','+','+','-','-','+','+','-','-','+','+','-','-','+','+',0},
  76.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
  77. }};
  78. static  char  lifespan[2][3][17] = {
  79. {
  80.   {'M','o','r','t','a','l',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  81.   {'I','m','m','o','r','t','a','l',' ',' ',' ',' ',' ',' ',' ',' ',0},
  82.   {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0},
  83. },
  84. {
  85.   {'+','-','-','+','+','-','-','+','+','-','-','+','+','-','-','+',0},
  86.   {'-','-','-','-','-','-','-','-','+','+','+','+','+','+','+','+',0},
  87.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
  88. }};
  89. static  char  outcode[3][17] = {
  90.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
  91.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0},
  92.   {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.',0}
  93. };
  94. #define BSB_NAME  0
  95. #define BSB_SPECIES 1
  96. #define BSB_LIFESPAN  2
  97. #define BSB_ENTER 3
  98. #define BSB_QUIT  4
  99. #define BSB_OUTPUT  5
  100. /* Local menu definitions */
  101.  
  102. #define STR1 "Socrates        "
  103. #define STR2 "++++++++--------"
  104. #define STR3 "Man             "
  105. #define STR4 "+-+-+-+-+-+-+-+-"
  106. #define STR5 "Mortal          "
  107. #define STR6 "+--++--++--++--+"
  108. #define STR7 "Enter"
  109. #define STR8 "Quit"
  110. #define STR9 "................ ................ ................"
  111. #define NULL_STR 0
  112. #ifdef MAC  /* Mac cannot assign pointers at compile time in code resources */
  113.  
  114. static GMENU_ITEM BsbList[] = {
  115.     { BSB_NAME,   NULL_STR, NULL_STR },
  116.     { BSB_SPECIES,  NULL_STR, NULL_STR },
  117.     { BSB_LIFESPAN, NULL_STR, NULL_STR },
  118.     { BSB_ENTER,  NULL_STR, NULL_STR  },
  119.     { BSB_QUIT,   NULL_STR, NULL_STR },
  120.     { BSB_OUTPUT, NULL_STR, NULL_STR, 0x0002 }
  121. };
  122.  
  123. #else /* MAC */
  124.  
  125. static GMENU_ITEM BsbList[] = {
  126.     { BSB_NAME,   "Socrates        ", "++++++++--------" },
  127.     { BSB_SPECIES,  "Man             ", "+-+-+-+-+-+-+-+-" },
  128.     { BSB_LIFESPAN, "Mortal          ", "+--++--++--++--+" },
  129.     { BSB_ENTER, "Enter", ""  },
  130.     { BSB_QUIT, "Quit" , "" },
  131.     { BSB_OUTPUT, "................ ................ ................",
  132.       "", 0x0002 }
  133. };
  134. #endif /* MAC */
  135.  
  136. static GMENU  InpMenu  = {  /* Input menu */
  137.   0,
  138.   3,        /* 3 items */
  139.   1,        /* key */
  140.   0x0001        /* auxillary line of text */
  141. };
  142.  
  143. static GMENU  UtMenu  = { /* Utility menu */
  144.   0,
  145.   2,        /* 2 items */
  146.   2,        /* key */
  147.   0x0000
  148. };
  149.  
  150. static GMENU  OutMenu  = {  /* Output menu */
  151.   0,
  152.   1,        /* 1 item */
  153.   3,        /* key */
  154.   0x0000
  155. };
  156.  
  157. static int  x_inp,y_inp,x_out,y_out,x_ut,y_ut; /* menu window positions */
  158.  
  159. /************************************************************************
  160.  *                  *
  161.  *  UsrIO - user I/O routine to handle requests from NWORKS   *
  162.  *                  *
  163.  ************************************************************************
  164.  */
  165.  
  166. static int InitFlag = 0;    /* initialize things flag */
  167.  
  168. void UsrIO()     /* handle NWORKS requests */
  169. {
  170.     GMENU_ITEM  *gmip;
  171.     char *sp;         /* string pointer */
  172.     int   wx, wy;       /* work indices */
  173.     int   key, xp, yp, button;      /* mouse interface */
  174.     int  *ci, nw;
  175.     int   xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
  176.  
  177.     if ( InitFlag == 0 ) {
  178.         /* open any files which may be required at this point in the
  179.      code. */
  180.   /* Get screen parameters */
  181.  
  182.     InpMenu.item = &BsbList[BSB_NAME];
  183.     UtMenu.item  = &BsbList[BSB_ENTER];
  184.     OutMenu.item  = &BsbList[BSB_OUTPUT];
  185. #ifdef MAC  
  186.     BsbList[0].text = STR1;
  187.     BsbList[0].text2 = STR2;
  188.     BsbList[1].text = STR3;
  189.     BsbList[1].text2 = STR4;
  190.     BsbList[2].text = STR5;
  191.     BsbList[2].text2 = STR6;
  192.     BsbList[3].text = STR7;
  193.     BsbList[4].text = STR8;
  194.     BsbList[5].text = STR9;
  195. #endif
  196.   ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
  197.   
  198.   if ( ncolor < 8 ) {
  199.     gm_intcolor = 1;
  200.     gm_txtcolor = 0;
  201.     gm_outcolor = 0;
  202.   } else {
  203.     gm_intcolor = 7;
  204.     gm_txtcolor = 4;
  205.     gm_outcolor = 0;
  206.   }
  207.  
  208.         /* Initialize menus and set window positions */
  209.   InitGMenu( &InpMenu, chrx, chry );
  210.   InitGMenu( &OutMenu, chrx, chry );
  211.   InitGMenu( &UtMenu, chrx, chry );
  212.   x_inp = (xsize - InpMenu.x1) / 2;
  213.   x_out = (xsize - OutMenu.x1) / 2;
  214.   x_ut = (xsize - UtMenu.x1) / 2;
  215.   y_ut = ysize / 3;
  216.   y_out = y_ut + 30;
  217.   y_inp = y_out + 30;
  218.  
  219.   
  220.   InpMenu.x0 = 0; /* Menu position relative to window */
  221.   InpMenu.y0 = 0;
  222.   OutMenu.x0 = 0; /* Menu position relative to window */
  223.   OutMenu.y0 = 0;
  224.   UtMenu.x0 = 0;  /* Menu position relative to window */
  225.   UtMenu.y0 = 0;
  226.  
  227.   InitFlag = 1;
  228.     }
  229.  
  230.     IORTNCDE = 0;       /* good return for data */
  231.     switch( IOREQCDE ) {
  232.     case RQ_ATTENTION:
  233.   /* This is invoked at the request of the user from the
  234.      execute menu.  It allows parameters to be changed or
  235.      altered.  Any graphics or other interactions are allowable
  236.      as usual for the other options.
  237.   */
  238.   break;
  239.  
  240.     case RQ_LSTART:       /* starting learn */
  241.   /* This tells the user that the program is about to start
  242.      learning.  It is called once for a LEARN ALL, LEARN ONE,
  243.      LEARN N, or LEARN START
  244.   */
  245.   break;
  246.  
  247.     case RQ_LEARNIN:        /* read training input */
  248.   /* IODATA points to an empty array of IOCOUNT elements.  The
  249.      values placed in this array by the user will become the
  250.      inputs to the network for training purposes.
  251.   */
  252.     case RQ_READ:       /* read test data */
  253.   /* IODATA points to an empty array of IOCOUNT values.  The
  254.      user must fill in these values.  The elements of the
  255.      array will become the "sum" of the inputs to the input
  256.      layer of processing elements.
  257.      */ 
  258.  
  259.   ug_window( InpMenu.key, gm_intcolor, x_inp, y_inp,
  260.        x_inp + InpMenu.x1 + 2, y_inp + InpMenu.y1 + 2);
  261.   ug_window( UtMenu.key, gm_intcolor, x_ut, y_ut,
  262.        x_ut + UtMenu.x1 + 2, y_ut + UtMenu.y1 + 2);
  263.  
  264.   DispGMenu( &InpMenu );
  265.   DispGMenu( &UtMenu );
  266.   for ( ; ; ) {
  267.     while ( (gmip=LookGMenu(&InpMenu,&button)) != (GMENU_ITEM *) 0 ) {
  268.       switch( gmip->code ) {
  269.       case BSB_NAME:
  270.         if (button == MBUT_RIGHT || button == MBUT_LEFT) {
  271.     ci = &curr_index[BSB_NAME];
  272.     nw = num_words[BSB_NAME];
  273.     if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
  274.     else *ci = (*ci + nw - 1) % nw;
  275.     gmip->text = name[0][*ci];
  276.     gmip->text2 = name[1][*ci];
  277.     DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
  278.         }
  279.         break;
  280.       case BSB_SPECIES:
  281.         if (button == MBUT_RIGHT || button == MBUT_LEFT) {
  282.     ci = &curr_index[BSB_SPECIES];
  283.     nw = num_words[BSB_SPECIES];
  284.     if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
  285.     else *ci = (*ci + nw - 1) % nw;
  286.     gmip->text = species[0][*ci];
  287.     gmip->text2 = species[1][*ci];
  288.     DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
  289.         }
  290.         break;
  291.       case BSB_LIFESPAN:
  292.         if (button == MBUT_RIGHT || button == MBUT_LEFT) {
  293.     ci = &curr_index[BSB_LIFESPAN];
  294.     nw = num_words[BSB_LIFESPAN];
  295.     if (button == MBUT_LEFT ) *ci = (*ci + 1) % nw;
  296.     else *ci = (*ci + nw - 1) % nw;
  297.     gmip->text = lifespan[0][*ci];
  298.     gmip->text2 = lifespan[1][*ci];
  299.     DispGItem( &InpMenu, gmip, gm_intcolor, gm_txtcolor );
  300.         }
  301.         break;
  302.       }
  303.       /* If button was pressed, wait for it to be released */
  304.       while ( button != 0 )
  305.         ug_mouse( &key, &xp, &yp, &button );
  306.     }
  307.     while ( (gmip=LookGMenu( &UtMenu, &button )) != (GMENU_ITEM *) 0 ) {
  308.       switch( gmip->code ) {
  309.       case BSB_ENTER:
  310.         /* Fill in IODATA with bit patterns for current input */
  311.         if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
  312.     for ( wy = 0, wx = 0; wy < 3; wy++ ) {
  313.       if (wy == 0 ) sp = name[1][curr_index[wy]];
  314.       else if (wy == 1) sp = species[1][curr_index[wy]];
  315.       else sp = lifespan[1][curr_index[wy]];
  316.       for( ; wx<(wy+1)*16; wx++, sp++ ) {
  317.         if ( *sp == '+' ) IODATA[wx] = 1.0;
  318.         else if ( *sp == '-' ) IODATA[wx] = -1.0;
  319.         else IODATA[wx] = 0.0;
  320.       }
  321.     }
  322.     goto end_read;
  323.         }
  324.         break;
  325.       case BSB_QUIT:
  326.         if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
  327.     IORTNCDE = -1;
  328.     goto end_read;
  329.         }
  330.         break;
  331.       }
  332.       /* If button was pressed, wait for it to be released */
  333.       while ( button != 0 )
  334.         ug_mouse( &key, &xp, &yp, &button );
  335.     }
  336.   }
  337. end_read:
  338.   break;
  339.  
  340.     case RQ_LEARNOUT:       /* read desired output */
  341.   /* IODATA points to an empty array of IOCOUNT values.  The
  342.      elements of the array will become the desired outputs for
  343.      training purposes.  These desired outputs correspond to
  344.      the most recent "RQ_LEARNIN" request.
  345.   */
  346.  
  347.   break;
  348.  
  349.     case RQ_WRITE:        /* write out results */
  350.   /* IODATA points to an array of IOCOUNT "float" type values.
  351.      The values are the outputs of the top-most layer of the
  352.      network.
  353.   */
  354.   IORTNCDE = 1;
  355.   break;
  356.  
  357.     case RQ_LEARNRSLT:
  358.   /* IODATA points to an array of IOCOUNT values.  These are the
  359.      output of the network caused by the inputs from RQ_LEARNIN.
  360.   */
  361.   ug_window( OutMenu.key, gm_intcolor, x_out, y_out,
  362.        x_out + OutMenu.x1 + 2, y_out + OutMenu.y1 + 2);
  363.  
  364.   sp = BsbList[BSB_OUTPUT].text;
  365.   for ( wy = 0, wx = 0; wy < 3; wy++ ) {
  366.     if ( wy != 0 ) *sp++ = ' ';
  367.     for( ; wx<(wy+1)*16; wx++, sp++ ) {
  368.       if (IODATA[wx] > 0.0) *sp = '+';
  369.       else if (IODATA[wx] < 0.0) *sp = '-';
  370.       else *sp = '.';
  371.     }
  372.   }
  373.   DispGMenu( &OutMenu );
  374.  
  375.   /* Lock Items */
  376.   BsbList[BSB_QUIT].flag |= GM_LOCKED;
  377.   BsbList[BSB_NAME].flag |= GM_LOCKED;
  378.   BsbList[BSB_SPECIES].flag |= GM_LOCKED;
  379.   BsbList[BSB_LIFESPAN].flag |= GM_LOCKED;
  380.   PutStr( "Select ENTER to continue\n" );
  381.   for ( ; ; ) {
  382.     while ( (gmip=LookGMenu( &UtMenu, &button )) == (GMENU_ITEM *)0 ) ;
  383.     if ( button == MBUT_RIGHT || button == MBUT_LEFT ) break;
  384.   }
  385.   /* Unlock Items */
  386.   BsbList[BSB_QUIT].flag &= ~GM_LOCKED;
  387.   BsbList[BSB_NAME].flag &= ~GM_LOCKED;
  388.   BsbList[BSB_SPECIES].flag &= ~GM_LOCKED;
  389.   BsbList[BSB_LIFESPAN].flag &= ~GM_LOCKED;
  390.   IORTNCDE = 1;
  391.   break;
  392.  
  393.     case RQ_WRSTEP:       /* write interim results */
  394.   /* each recall cycle for the Hopfield and BAM control strategies,
  395.      the intermediate output is made available to userio to test
  396.      for convergence or other desired states.  This option is
  397.      not used for other control strategies.
  398.   */
  399.  
  400.   ug_window( OutMenu.key, gm_intcolor, x_out, y_out,
  401.        x_out + OutMenu.x1 + 2, y_out + OutMenu.y1 + 2);
  402.  
  403.   sp = BsbList[BSB_OUTPUT].text;
  404.   for ( wy = 0, wx = 0; wy < 3; wy++ ) {
  405.     if ( wy != 0 ) *sp++ = ' ';
  406.     for( ; wx<(wy+1)*16; wx++, sp++ ) {
  407.       if (IODATA[wx] > 0.0) *sp = '+';
  408.       else if (IODATA[wx] < 0.0) *sp = '-';
  409.       else *sp = '.';
  410.     }
  411.   }
  412.   DispGMenu( &OutMenu );
  413.  
  414.   /* Lock Items */
  415.   BsbList[BSB_NAME].flag |= GM_LOCKED;
  416.   BsbList[BSB_SPECIES].flag |= GM_LOCKED;
  417.   BsbList[BSB_LIFESPAN].flag |= GM_LOCKED;
  418.   PutStr( "Select ENTER to continue, QUIT to terminate recall\n" );
  419.   for ( ; ; ) {
  420.     while ( (gmip=LookGMenu(&UtMenu,&button )) == (GMENU_ITEM *)0 ) ;
  421.     switch( gmip->code ) {
  422.     case BSB_ENTER:
  423.       if ( button == MBUT_RIGHT || button == MBUT_LEFT ) goto end_wr;
  424.       break;
  425.     case BSB_QUIT:
  426.       if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
  427.         IORTNCDE = -1;
  428.         goto end_wr;
  429.       }
  430.       break;
  431.     }
  432.   }
  433. end_wr:
  434.   /* Unlock Items */
  435.   BsbList[BSB_NAME].flag &= ~GM_LOCKED;
  436.   BsbList[BSB_SPECIES].flag &= ~GM_LOCKED;
  437.   BsbList[BSB_LIFESPAN].flag &= ~GM_LOCKED;
  438.  
  439.   break;
  440.   
  441.     case RQ_RSTART:       /* starting recall */
  442.   /* This tells the user that the program is about to start
  443.      a recall.  It is called once for a REACLL ALL, RECALL ONE,
  444.      RECALL N, or RECALL START.
  445.   */
  446.   break;
  447.  
  448.     case RQ_RCLTST:   /* read desired output during recall test */
  449.   /* IODATA points to an empty array of IOCOUNT values.  The
  450.      elements of the array will become the desired outputs for
  451.      recall purposes.  This request is only made during a
  452.      Execute Network/Recall Test.
  453.   */
  454.  
  455.   break;
  456.  
  457.     case RQ_TERM:       /* terminate interface */
  458.   /* close any files which may be open.  Deallocate any memory
  459.      which was allocated.  (This is VERY VERY important.  If
  460.      allocated memory is NOT released, dos memory will become
  461.      fragmented and it will become necessary to reboot.
  462.   */
  463.  
  464.   PutStr( "bye bye\n" );
  465.   break;
  466.     }
  467.  
  468.     return;
  469. }
  470.